package com.linkivr.action;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import com.linkivr.service.*;

import com.linkivr.util.BaseMd5;
import com.linkivr.util.GL;

@Component("LoginAction")
public class LoginAction extends MyAction {
	private UserService service;
	private static final long serialVersionUID = 1L;

	@Resource
	public void setService(UserService service) {
		this.service = service;
	}

	public UserService getService() {
		return service;
	}

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public String login() throws IOException {
		GL.info("login()函数  ");
		beginReqParam();

		GL.info("login()函数  " + getIpAddr(request));
		
		session = request.getSession(true);
		GL.info("sessionid=" + session.getId());
		session.setMaxInactiveInterval(-1);
		// 获取表达参数
		// String
		// String f_companyid = request.getParameter("f_companyid");
		// String f_usertype = request.getParameter("f_usertype");
		String f_userid = request.getParameter("f_userid");
		String f_password = request.getParameter("f_password");

		// info("f_companyid="+f_companyid);
		// info("f_usertype="+f_usertype);
		GL.info("f_userid=" + f_userid);
		GL.info("f_password=" + f_password);

		// session.setAttribute("s_companyid", f_companyid);
		// session.setAttribute("s_usertype", f_usertype);

		String pwd = BaseMd5.GetMd5SumStr(f_password.getBytes());
		boolean ok = service.checkAccount(f_userid, pwd);
		if ((!ok) && (service.queryCount() <= 0)) {// 目前系统中没有帐户
			if (f_userid.equals("admin") && f_password.equals("admin1234")) {
				ok = true;
			}
		}

		// todo:add code here
		// String loginfo = "";
		if (ok == true) {
			// loginfo = "登录成功";
			out.write("{'success':true}");

			session.setAttribute("s_userid", f_userid);
			session.setAttribute("s_password", f_password);
			session.setAttribute("s_userip", getIpAddr(request));

		} else {
			// loginfo = "登录失败";
			out.write("{'success':false}");

			session.setAttribute("s_userid", null);
		}

		endReqParam();

		return null;
	}
	
	public String login2() throws IOException {
		GL.info("login2()函数  ");
		beginReqParam();

		GL.info("login2()函数  " + getIpAddr(request));
		
		session = request.getSession(true);
		GL.info("sessionid=" + session.getId());
		session.setMaxInactiveInterval(-1);

		String f_userid = request.getParameter("f_userid");
		String f_password = request.getParameter("f_password");

		GL.info("f_userid=" + f_userid);
		GL.info("f_password=" + f_password);


		String pwd = BaseMd5.GetMd5SumStr(f_password.getBytes());
		boolean ok = service.checkAccount(f_userid, pwd);
		if ((!ok) && (service.queryCount() <= 0)) {// 目前系统中没有帐户
			if (f_userid.equals("admin") && f_password.equals("admin1234")) {
				ok = true;
			}
		}

		// todo:add code here
		// String loginfo = "";
		if (ok) {
			// loginfo = "登录成功";
			out.write("{'success':true}");

			session.setAttribute("s_userid", f_userid);
			session.setAttribute("s_password", f_password);
			session.setAttribute("s_userip", getIpAddr(request));

		} else {
			// loginfo = "登录失败";
			out.write("{'success':false}");

			session.setAttribute("s_userid", null);
		}
		
		if(ok){
			response.sendRedirect("../index.jsp");
		}else{
			out.write("<script type=text/javascript>window.top.location.href='../login.htm'</script>");
		}

		endReqParam();
		return null;
	}

	public String getLoginName() throws IOException {
		GL.info("getloginname()函数  ");

		beginReqParam();

		GL.info("getloginname()函数  " + getIpAddr(request));
		GL.info("sessionid=" + session.getId());

		// session.setAttribute("s_companyid", f_companyid);
		// session.setAttribute("s_usertype", f_usertype);
		String s_userid = "---";
		if (session.getAttribute("s_userid") != null) {
			s_userid = session.getAttribute("s_userid").toString();
			GL.info("getloginname()函数 ,用户名 " + s_userid);
			// s_userid = new String(s_userid.getBytes("ISO-8859-1"),"utf-8");
		}

		out.print(s_userid);

		endReqParam();

		return null;
	}

	public String logout() throws IOException {
		GL.info("logout()函数  ");
		beginReqParam();

		GL.info("logout()函数  " + getIpAddr(request));
		session = request.getSession(true);
		GL.info("sessionid=" + session.getId());

		// session.setAttribute("s_companyid", f_companyid);
		// session.setAttribute("s_usertype", f_usertype);
		session.setAttribute("s_userid", null);
		// response.sendRedirect("../login.htm");

		out.write("<script type=text/javascript>window.top.location.href='../login.htm'</script>");
		endReqParam();
		return null;
	}

}
